package cn.edu.zafu.bigdata.linfeifei.h;

import java.io.*;
import java.util.*;

public class StudentInfoSystem {
    private static final String FILE_NAME = "students.dat";
    private static Map<String, Student> studentMap = new HashMap<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        loadStudentsFromFile();
        boolean running = true;

        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    retrieveStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    running = false;
                    saveStudentsToFile();
                    System.out.println("程序已退出。");
                    break;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }
        scanner.close();
    }

    private static void addStudent() {
        System.out.print("请输入学生学号: ");
        String id = scanner.next();
        if (studentMap.containsKey(id)) {
            System.out.println("学号重复，请重新输入。");
            return;
        }
        System.out.print("请输入学生姓名: ");
        String name = scanner.next();
        System.out.print("请输入学生电话: ");
        String phone = scanner.next();
        Student student = new Student(id, name, phone);
        studentMap.put(id, student);
        System.out.println("学生信息已添加。");
    }

    private static void retrieveStudent() {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String input = scanner.next();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            Student student = studentMap.get(input);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String input = scanner.next();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            Student student = studentMap.get(input);
            if (student != null) {
                System.out.print("请输入新的学生姓名: ");
                String name = scanner.next();
                System.out.print("请输入新的学生电话: ");
                String phone = scanner.next();
                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息已更新。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入学生学号（输入'QUIT'退出）: ");
            String input = scanner.next();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            if (studentMap.remove(input) != null) {
                System.out.println("学生信息已删除。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void loadStudentsFromFile() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentMap = (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生信息，将创建一个新的存储。");
        }
    }

    private static void saveStudentsToFile() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentMap);
        } catch (IOException e) {
            System.out.println("无法保存学生信息。");
        }
    }
}